package com.xiaoye.qbdatahandle.service;

import com.hankcs.hanlp.summary.TextRankSentence;
import com.huaban.analysis.jieba.JiebaSegmenter;
import com.xiaoye.qbdatahandle.domian.QbSubjectArticle;
import com.xiaoye.qbdatahandle.entity.ClearStepEntity;
import com.xiaoye.qbdatahandle.util.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

// 内容清洗
@Service
public class TextHandleService {
    public static final Set<String> STOP_WORDS;

    static {
        STOP_WORDS = new HashSet<>();
        Resource resource = new ClassPathResource("stopWords.txt");
        try (

                BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
            String line;
            while ((line = br.readLine()) != null) {
                STOP_WORDS.add(line);  // 将每一行添加到 List 中
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 使用jieba分詞
     */
    public List<String> analyseText(String text) {
        // 使用 Jieba 进行中文分词
        JiebaSegmenter jiebaSegmenter = new JiebaSegmenter();
        // 精確模式
        List<String> words = jiebaSegmenter.sentenceProcess(text)
                .stream()
                .map(this::removePunctuationAndLinks)
                .collect(Collectors.toList());
        return words;
    }

    public List<String> analyseTextAndRemoveStop(String text) {
        // 使用 Jieba 进行中文分词
        JiebaSegmenter jiebaSegmenter = new JiebaSegmenter();
        // 精確模式
        List<String> words = jiebaSegmenter.sentenceProcess(text).stream()
                .map(String::toLowerCase)
                .map(word -> convertEncoding(word, "utf-8"))
                .filter(word -> !STOP_WORDS.contains(word))
                .collect(Collectors.toList());
        ;
        return words;
    }

    // 5. 清洗整个文本内容
    public String cleanText(String content, boolean toLowerCase, String targetCharset, QbSubjectArticle qbSubjectArticle) throws UnsupportedEncodingException {
        if (content == null || content.isEmpty()) {
            return null;
        }
        ClearStepEntity clearStep = new ClearStepEntity();
        qbSubjectArticle.setClearStep(clearStep);
        // 分词
        List<String> tokens = analyseText(content);
        //  记录 分词结果
        clearStep.setWordSegmentations(tokens);
        System.out.println("分词结果:" + tokens);
        // 记录 停用词
        clearStep.setStopWords(TextHandleService.STOP_WORDS);
        // 大小写记录
        Map<String, String> caseMap = clearStep.getCaseMap();
        // 字符编码转换记录
        Map<String, String> chartEncodingMap = clearStep.getChartEncodingMap();
        for (String beforeText : tokens) {
            if (StringUtils.isBlank(beforeText)) {
                continue;
            }
            // 转换大小写
            String caseAfter = convertCase(beforeText, toLowerCase);
            if (!beforeText.equals(caseAfter)) {
                caseMap.put(beforeText, caseAfter);
            }
            // 字符编码转换
            String chartEncodingBefore = convertEncoding(beforeText, targetCharset);
            if (!beforeText.equals(chartEncodingBefore)) {
                chartEncodingMap.put(beforeText, chartEncodingBefore);
            }

        }
        // 转换大小写
        List<String> analyseTextAndRemoveStop = analyseTextAndRemoveStop(content);
        // 6. 输出结果（可转为指定编码的字节流）
        String cleanedText = String.join("", analyseTextAndRemoveStop);
        return cleanedText;
    }

    // 移除标点符号和链接的方法
    private String removePunctuationAndLinks(String text) {
        // 定义正则表达式来匹配标点符号和链接
        String punctuationPattern = "[\\\\p{Punct}，。！？；：、“”‘’【】（）]+";
        String linkPattern = "\\bhttps?://[^\\s]+\\b";

        // 移除标点符号
        text = text.replaceAll(punctuationPattern, " ");

        // 移除链接
        text = text.replaceAll(linkPattern, " ");

        // 移除多余的空格
        text = text.replaceAll("\\s+", " ");

        return text.trim();
    }

    /**
     * 大小写转换
     */
    private String convertCase(String text, boolean toLowerCase) {
        return toLowerCase ? text.toLowerCase() : text.toUpperCase();
    }


    // 将字符串从源编码转换为目标编码
    public static String convertEncoding(String text, String targetEncoding) {
        byte[] sourceBytes = text.getBytes();
        return new String(sourceBytes, Charset.forName(targetEncoding));
    }

    /**
     * 生成摘要
     */
    public String getIntro(String content) {
        if (content == null || content.isEmpty()) {
            return "";
        }
        // 抽取关键句（参数：文本、摘要句子数）
        List<String> keySentences = TextRankSentence.getTopSentenceList(content, 3);
        return String.join(" ", keySentences);
    }
}
